package com.example.oauth2authserver.config;

import com.example.oauth2authserver.security.SecurityUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        final Map<String,Object> additionalInformation = new HashMap<>();
        SecurityUser user = (SecurityUser) authentication.getUserAuthentication().getPrincipal();
        additionalInformation.put("userId",user.getId());
        additionalInformation.put("username",user.getUsername());

        ((DefaultOAuth2AccessToken)accessToken).setAdditionalInformation(additionalInformation);
        return accessToken;
    }
}
